package com.qg.cyapp.component;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qg.cyapp.R;
import com.qg.cyapp.activity.MenuSingleActivity;
import com.qg.cyapp.bean.FoodBean;
import com.qg.cyapp.util.ShareUtil;

/**
 * @author xuan 2014-10-30
 * 
 */
public class MenuWaterFall extends WaterFall {

	private List<FoodBean> foods;
	private Context mContext;
	private LayoutInflater inflater;
	private ScrollToBottomListener toBottom;
	private int activity = 0;

	public MenuWaterFall(Context context, List<FoodBean> foods , int activity) {
		super(context);
		// TODO Auto-generated constructor stub
		this.foods = foods;
		this.activity = activity;
		this.mContext = context;
		this.inflater = LayoutInflater.from(context);
	}

	public MenuWaterFall(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void scrollToBottom() {
		// TODO Auto-generated method stub
		toBottom.scrollToBottom();
	}

	@Override
	public View getView(int position) {
		FoodBean food = foods.get(position);
		View view = inflater.inflate(R.layout.item_single_food, null);
		view.setTag(position);
		view.setOnClickListener(onClick);
		ImageView ivFood = (ImageView) view.findViewById(R.id.single_iv);
		TextView tvFoodName = (TextView) view.findViewById(R.id.tv_food_name);
		TextView tvFoodPrice = (TextView) view.findViewById(R.id.tv_food_price);
		ShareUtil.imageLoader.displayImage(food.picture, ivFood, null, false);
		tvFoodName.setText(food.name);
		tvFoodPrice.setText(String.valueOf(food.price) + "元");
		return view;
	}

	@Override
	public int getContentChildCount() {
		return foods.size();
	}

	OnClickListener onClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(mContext, MenuSingleActivity.class);
			int position = (Integer) v.getTag();
			intent.putExtra(ShareUtil.ACTIVITY_KEY,
					activity);
			intent.putExtra(ShareUtil.POSITION_KEY, position);
			mContext.startActivity(intent);
		}
	};

	public void setScrollBottomListener(ScrollToBottomListener toBottom) {
		this.toBottom = toBottom;
	}

	@Override
	public int getShowNum() {
		// TODO Auto-generated method stub
		return 6;
	}

	public interface ScrollToBottomListener {
		public void scrollToBottom();
	}

	@Override
	public Object getObject(int position) {
		// TODO Auto-generated method stub
		return foods.get(position);
	}
}
